package com.scwatch.core.domain;

import com.scwatch.common.web.Anchor;
import com.scwatch.ext.domain.LiveBroadcast;

import javax.persistence.*;
import java.util.Collection;

/**
 * InfoComment
 * 
 * @author gjl
 * 
 */
@Entity
@DiscriminatorValue("Live")
public class LiveComment extends Comment {
	private static final long serialVersionUID = 1L;

	@Override
	@Transient
	public Anchor getAnchor() {
		return live;
	}

	private LiveBroadcast live;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "f_fid", nullable = false, insertable = false, updatable = false)
	public LiveBroadcast getLive() {
		return live;
	}

	public void setLive(LiveBroadcast live) {
		this.live = live;
	}


    @Transient
    public void addComments(int comments) {
//        Set<InfoBuffer> buffers = getBuffers();
//        if (buffers == null) {
//            buffers = new HashSet<InfoBuffer>(1);
//            setBuffers(buffers);
//        }
//        InfoBuffer buffer;
//        if (buffers.isEmpty()) {
//            buffer = new InfoBuffer();
//            buffer.applyDefaultValue();
//            buffer.setInfo(this);
//            buffers.add(buffer);
//        } else {
//            buffer = buffers.iterator().next();
//        }
//        // 根据设置处理缓冲。
//        Integer origComments = getComments();
//        Integer bufferComments = buffer.getComments() + comments;
//        if (bufferComments >= getSite().getGlobal().getOther().getBufferInfoComments()) {
//            buffer.setComments(0);
//            setComments(origComments + bufferComments);
//        } else {
//            buffer.setComments(bufferComments);
//        }
    }

    /**
     * 获得评论状态
     */
    @Transient
    public int getCommentStatus(User user, Collection<MemberGroup> groups) {
//        SiteComment conf = getSite().getConf(SiteComment.class);
//        int mode = conf.getMode();
//        if (mode == SiteComment.MODE_OFF || !isNormal() || isLinked()) {
//            return SiteComment.STATUS_OFF;
//        }
//        Boolean allow = getAllowComment();
//        if (allow != null && !allow) {
//            return SiteComment.STATUS_OFF;
//        }
//        if (user == null && mode == SiteComment.MODE_USER) {
//            return SiteComment.STATUS_LOGIN;
//        }
//        if (allow != null && allow) {
//            return SiteComment.STATUS_ALLOWED;
//        }
//        Set<MemberGroup> commentGroups = getNode().getCommentGroups();
//        if (Reflections.containsAny(commentGroups, groups, "id")) {
//            return SiteComment.STATUS_ALLOWED;
//        } else {
//            if (user != null) {
//                return SiteComment.STATUS_DENIED;
//            } else {
//                return SiteComment.STATUS_LOGIN;
//            }
//        }
        return  0;
    }
}
